home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / python-support / gnome-games-data / glchess / scene / human.py < prev   
Encoding:
Python Source  |  2009-04-14  |  4.3 KB  |  160 lines

  1. # -*- coding: utf-8 -*-
  2. """
  3. """
  4.  
  5. __author__ = 'Robert Ancell <bob27@users.sourceforge.net>'
  6. __license__ = 'GNU General Public License Version 2'
  7. __copyright__ = 'Copyright 2005-2006  Robert Ancell'
  8.  
  9. import glchess.scene
  10.  
  11. class SceneHumanInput:
  12.     """
  13.     """
  14.     # Flag to control if human input is enabled
  15.     __inputEnabled = True
  16.  
  17.     # The selected square to move from
  18.     __startSquare = None
  19.     
  20.     __showHints       = True
  21.     
  22.     def __init__(self):
  23.         """Constructor for a scene with human selectable components"""
  24.         pass
  25.     
  26.     # Methods to extend
  27.     
  28.     def onRedraw(self):
  29.         """This method is called when the scene needs redrawing"""
  30.         pass
  31.         
  32.     def playerIsHuman(self):
  33.         """Check if the current player is a human.
  34.         
  35.         Return True the current player is human else False.
  36.         """
  37.         return False
  38.     
  39.     def getSquare(self, x, y):
  40.         """Find the chess square at a given 2D location.
  41.         
  42.         'x' is the number of pixels from the left of the scene to select.
  43.         'y' is the number of pixels from the bottom of the scene to select.
  44.         
  45.         Return the co-ordinate as a tuple in the form (file,rank) or None if
  46.         no square at this point.
  47.         """
  48.         pass
  49.  
  50.     def squareIsFriendly(self, coord):
  51.         """Check if a given square contains a friendly piece.
  52.         
  53.         Return True if this square contains a friendly piece.
  54.         """
  55.         return False
  56.     
  57.     def canMove(self, start, end):
  58.         """Check if a move is valid.
  59.         
  60.         'start' is the location to move from in LAN format (string).
  61.         'end' is the location to move from in LAN format (string).
  62.         """
  63.         return False
  64.     
  65.     def moveHuman(self, start, end):
  66.         """Called when a human player moves.
  67.         
  68.         'start' is the location to move from in LAN format (string).
  69.         'end' is the location to move from in LAN format (string).
  70.         """
  71.         pass
  72.     
  73.     # Public methods
  74.     
  75.     def enableHumanInput(self, inputEnabled):
  76.         """Enable/disable human input.
  77.         
  78.         'inputEnabled' is a flag to show if human input is enabled (True) or disabled (False).
  79.         """
  80.         if inputEnabled is False:
  81.             self.__selectSquare(None)
  82.         self.__inputEnabled = inputEnabled
  83.         
  84.     def select(self, x, y):
  85.         """
  86.         """
  87.         if self.__inputEnabled is False:
  88.             return
  89.         
  90.         # Only bother if the current player is human
  91.         if self.playerIsHuman() is False:
  92.             return
  93.         
  94.         # Get the selected square
  95.         coord = self.getSquare(x, y)
  96.         if coord is None:
  97.             return
  98.         
  99.         # Deselect when clicking on piece a second time
  100.         if self.__startSquare == coord:
  101.             self.__selectSquare(None)
  102.             return
  103.  
  104.         # If this is a friendly piece then select it
  105.         if self.squareIsFriendly(coord):
  106.             self.__selectSquare(coord)
  107.  
  108.         else:
  109.             # If we have already selected a start move try
  110.             # and move to this square
  111.             if self.__startSquare is not None:
  112.                 self.__move(self.__startSquare, coord)
  113.  
  114.         # Redraw the scene
  115.         self.onRedraw()
  116.         
  117.         return coord
  118.  
  119.     def deselect(self, x, y):
  120.         """
  121.         """
  122.         if self.__inputEnabled is False:
  123.             return
  124.         
  125.         # Only bother if the current player is human
  126.         if self.playerIsHuman() is False:
  127.             return
  128.         
  129.         # Get the selected square
  130.         coord = self.getSquare(x, y)
  131.         if coord is None:
  132.             return
  133.         
  134.         # Attempt to move here
  135.         if self.__startSquare is not None and self.__startSquare != coord:
  136.             self.__move(self.__startSquare, coord)
  137.         
  138.         # Redraw the scene
  139.         self.onRedraw()
  140.         
  141.         return coord
  142.     
  143.     # Private methods
  144.     
  145.     def __selectSquare(self, coord):
  146.         if self.__startSquare == coord:
  147.             return
  148.         self.__startSquare = coord
  149.         self.selectSquare(coord)
  150.     
  151.     def __move(self, start, end):
  152.         """Attempt to make a move.
  153.         
  154.         ...
  155.         """
  156.         if self.canMove(start, end) is False:
  157.             return
  158.         self.__selectSquare(None)
  159.         self.moveHuman(start, end)
  160.